﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    public class Order_InformationController : Controller
    {
        //
        // GET: /Order_Information/
        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取商城信息数据
        /// </summary>
        /// <param name="jqgridparam">datagrid页面参数</param>
        /// <returns></returns>
        public ActionResult getgrids(JqGridParam jqgridparam)
        {
            try
            {
                Page<Order_Information_T> page = new Page<Order_Information_T>();//分页类定义
                List<Order_Information_T> list = new List<Order_Information_T>();//定义商品分类设置 
                string result = service.Get_Order_Information("", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Order_Information_T m = new Order_Information_T
                            {
                                InformationID = item["InformationID"].ToString(),
                                UserID = Int32.Parse(item["UserID"].ToString()),
                                Type = item["Type"].ToString(),
                                Title = item["Title"].ToString(),
                                Contents = item["Contents"].ToString(),
                                UpdateTime = item["UpdateTime"].ToString()
                            };
                            list.Add(m);
                        }
                    }
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)
                    };
                    return Json(data);
                }
                else if (bs == "0")
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    return Json(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex.Message + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 增、删、改商城信息
        /// </summary>
        /// <param name="Tag"></param>
        /// <param name="Order_Information"></param>
        /// <returns></returns>
        public ActionResult UpdateOrder_Information(int Tag, Order_Information_T Order_Information)
        {
            try
            {
                string result = "";
                View_OperUser user = (View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }
                #region 转换传入的信息
                Order_Information_T New_Information = new Order_Information_T
                {
                    InformationID = Order_Information.InformationID,
                    UserID = Order_Information.UserID,
                    Type = Order_Information.Type,
                    Title = Order_Information.Title,
                    Contents = Order_Information.Contents.Replace('≮', '<').Replace('≯', '>'),
                    UpdateTime = Order_Information.UpdateTime,
                };
                #endregion
                string a = New_Information.ToJson();
                result = service.SetOrder_information(user.OperID, New_Information.ToJson(), Tag, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex + "请联系管理员解决");
            }
        }

    }
}
